package com.example.lsrsbackend.mapper;

import com.example.lsrsbackend.model.Student;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface StudentMapper {

    @Select("SELECT * FROM students WHERE username = #{username} AND password = #{password}")

    Student findByUsernameAndPassword(String username, String password);

    @Insert("INSERT INTO students " +
            "(username, password, grade, education, college, major, phone, email, conments) " +
            "VALUES (#{username}, #{password}, #{grade}, #{education}, #{college}, #{major}, #{phone}, #{email}, #{conments})")
    @Options(useGeneratedKeys = true, keyProperty = "id")   // 回写自增主键
    void insertStudent(Student student);

    //修改密码
    @Update("UPDATE students SET password = #{newPassword} WHERE username = #{username}")
    int updatePasswordByUsername(@Param("username") String username,
                                 @Param("newPassword") String newPassword);


    //根据username获取数据
    @Select("SELECT * FROM students WHERE username = #{username}")
    Student findByUsername(String username);

    //部分更新 : 动态更新学生信息
    @Update({"<script>",
            "UPDATE students",
            "<set>",
            "<if test='grade != null'>grade = #{grade},</if>",
            "<if test='education != null'>education = #{education},</if>",
            "<if test='college != null'>college = #{college},</if>",
            "<if test='major != null'>major = #{major},</if>",
            "<if test='phone != null'>phone = #{phone},</if>",
            "<if test='email != null'>email = #{email},</if>",
            "</set>",
            "WHERE username = #{username}",
            "</script>"})
    int updateStudentInfo(Student student);


    //获取全部学生信息
    @Select("SELECT * FROM students")
    List<Student> getAllStudents();

    // 添加删除方法
    @Delete("DELETE FROM students WHERE username = #{username}")
    int deleteByUsername(String username);

    /* 冻结 */
    @Update("UPDATE students SET status = 'frozen' WHERE username = #{username}")
    int freezeByUsername(@Param("username") String username);

    /* 解冻 */
    @Update("UPDATE students SET status = 'active' WHERE username = #{username}")
    int unfreezeByUsername(@Param("username") String username);

    /* 根据用户名查询状态（用于后续判断） */
    @Select("SELECT status FROM students WHERE username = #{username}")
    String selectStatusByUsername(@Param("username") String username);
}